window.IM_CHAT = {
	socket: null,
	ImChatMsgType:{
		CONNECT:0,
		TEXT:1,
		IMG:2,
		VOICE:3,
		VIDEO:4
	},
	init: function(imUser) {
		if (window.WebSocket) {		
			if(!wsWebSocketUrl){
				layer.msg("im服务地址不能为空");
				return false;
			}
			
			IM_CHAT.socket = new WebSocket(wsWebSocketUrl);
			IM_CHAT.socket.onopen = function() {
				$(".im-connect-status").text("连接成功..");
				IM_CHAT.initConnect(imUser);	
			},	
			IM_CHAT.socket.onclose = function() {
				$(".im-connect-status").text("连接关闭..");
			},			
			IM_CHAT.socket.onerror = function() {	
				$(".im-connect-status").text("发生错误..");
			},	
			IM_CHAT.socket.onmessage = function(e) {
				console.log("接受到消息：" + e.data);		
				
				// 显示日志	
				IM_CHAT.showImMsgHtml(imUser,e.data,".msg-logs-key .im-chat-msg-logs",".layim-chat-logs-main.msg-logs-key");
			}
		} else {
			alert("浏览器不支持websocket协议...");
		}
	},
	initConnect: function(imUser){
		// 初始化链接
		// 封装消息 
		var msgObj = IM_CHAT.initMsgObj(imUser,null,null,IM_CHAT.ImChatMsgType.CONNECT);
		// 发送消息
		var msgObjJson = JSON.stringify(msgObj);
		IM_CHAT.socket.send(msgObjJson);
	},
	chatSendMsg: function(imUser,friendId,msg) {
		if (!msg) {	
			layer.msg("消息不能为空");
			return false;
		}
		// 验证消息 
		IM_CHAT.beforeMsgAuth(imUser,friendId,msg);
		// 封装消息 
		var msgObj = IM_CHAT.initMsgObj(imUser,friendId,msg,IM_CHAT.ImChatMsgType.TEXT);
		// 发送消息
		var msgObjJson = JSON.stringify(msgObj);
		IM_CHAT.socket.send(msgObjJson);
	},
	chatSendGroupMsg: function(imUser ,groupId ,msg) {
		if (!msg) {	
			layer.msg("消息不能为空");
			return false;
		}
		var msgObj = IM_CHAT.initGroupMsgObj(imUser,groupId,msg,IM_CHAT.ImChatMsgType.TEXT);
		// 发送消息
		var msgObjJson = JSON.stringify(msgObj);
		IM_CHAT.socket.send(msgObjJson);
	},	
	beforeMsgAuth: function(imUser,friendId,msg){
		if (!imUser) {
			layer.msg("请先登录,获取用户失败");
			return false;
		}
		if (!friendId) {
			layer.msg("请先登录,好友不能为空");
			return false;
		}
		if (!msg) {	
			layer.msg("消息不能为空");
			return false;
		}
		// 清空消息
		$(".sed-msg-friend-id-"+friendId).val("")
	},
	initMsgObj: function(imUser,friendId,msg,msgType){
		// 消息对象
		var msgObj = {
			sendId:imUser.userId,
			sendAvatar:imUser.headImgUrl,
			sendName:imUser.nickname,
			receiveId:friendId,
			msgContent:msg,
			msgType:msgType,
			imType:1
		}
		return msgObj;
	},	
	initGroupMsgObj: function(imUser,groupId,msg,msgType){
		// 消息对象	
		var msgObj = {
			sendId:imUser.userId,
			sendAvatar:imUser.headImgUrl,
			sendName:imUser.nickname,
			groupId:groupId,
			msgContent:msg,
			msgType:msgType,
			imType:2
		}
		return msgObj;
	},
	showImMsgHtml: function(currentUser,msgData,appendKey,scrollTopKey){
		var _currentUserId = currentUser.userId;
		var _msgDataObj = JSON.parse(msgData);
		
		var _msgObj = _msgDataObj.data;
		var _sendId = _msgObj.sendId;
		var _receiveId = _msgObj.receiveId;
		var _msgContent = _msgObj.msgContent;
		var _sendTime = _msgObj.sendTime;
		var _msgType = _msgObj.msgType;
		var _sendAvatar = _msgObj.sendAvatar;
		var _sendName = _msgObj.sendName;
		// 消息类型 1、好友 ， 2、群聊
		var _imType = _msgObj.imType;
		
		var _html = "";	
		if(_msgType!=0){
			if(_currentUserId==_sendId){
				// 自己发送
				_html+='<li class="layim-chat-mine"> ';
				_html+='<div class="layim-chat-user">';
				_html+='<img src="'+currentUser.headImgUrl+'">';
				_html+='<cite><i>'+commonDateFormat(_sendTime,"yyyy-MM-dd hh:mm:ss")+'</i>'+currentUser.nickname+'</cite>';
				_html+='</div>';	
				_html+='<div class="layim-chat-text" style="background-color: #00c1de;color: #efeff4;">'+_msgContent+'</div>';
				_html+='</li>';	
			}else{
				// 
				_html+='<li>';	
				_html+='<div class="layim-chat-user">';	
				_html+='<img src="'+_sendAvatar+'">';
				_html+='<cite>'+_sendName+'<i>'+commonDateFormat(_sendTime,"yyyy-MM-dd hh:mm:ss")+'</i></cite>';
				_html+='</div>';		
				_html+='<div class="layim-chat-text">'+_msgContent+'</div>';
				_html+='</li>';	
			}	
						
			if(_msgObj.msgCallCack==true||(_currentUserId!=_sendId)){
				if(appendKey){	
					$(appendKey).append(_html);	
				}
				if(scrollTopKey){	
					$(scrollTopKey).scrollTop($(scrollTopKey)[0].scrollHeight);
				}
			}
				
			// 好友消息 	
			if(_imType==1){			
				IM_CHAT.showFriendMsgHtml(currentUser,_msgObj,_html);
			}
			
			// 群聊消息			
			if(_imType==2 && _msgObj.msgCallCack==false){
				IM_CHAT.showGroupMsgHtml(currentUser,_msgObj,_html);
			}
		}
	},
	showFriendMsgHtml: function(currentUser,msgObj,html){
		console.log("好友消息");
		
		var _appendKey = ".im-chat-msg-friend-val-";
		var _scrollTopKey = ".im-chat-msg-friend-key-";	
		if(currentUser.userId == msgObj.sendId){
			// 消息发送-成功	
			_appendKey += msgObj.receiveId;
			_scrollTopKey += msgObj.receiveId;	
			$(_appendKey).append(html);			
			$(_scrollTopKey).scrollTop($(_scrollTopKey)[0].scrollHeight);
			
		}else if(currentUser.userId == msgObj.receiveId){
			// 收到别人发送的消息
			_appendKey += msgObj.sendId;
			_scrollTopKey += msgObj.sendId;
			// 如果是收到的消息  需要判断弹窗有没有打开 
			console.log("收到好友消息：_appendKey = " +_appendKey);
			console.log("收到好友消息：_scrollTopKey = " +_scrollTopKey)
			var _open = $(_appendKey).attr("open");	
			console.log("收到好友消息：open = " +_open);
			if(typeof(_open) == "undefined"){
				// 需要手动打开弹窗
				openFriendImMsgPopup(msgObj.sendId,msgObj.sendName);
						
				//$(_appendKey).append(html);			
				//$(_scrollTopKey).scrollTop($(_scrollTopKey)[0].scrollHeight);
			}else{
				$(_appendKey).append(html);			
				$(_scrollTopKey).scrollTop($(_scrollTopKey)[0].scrollHeight);
			}
		}else {	
			// 消息发送失败		
			_appendKey += msgObj.receiveId;
			_scrollTopKey += msgObj.receiveId;
			console.log("好友消息：消息发送失败 = " +_appendKey);
		}
	},
	showGroupMsgHtml: function(currentUser,msgObj,html){
		console.log("群聊消息");
		// 						.msg-logs-key .im-chat-msg-logs
		// .layim-chat-logs-main.msg-logs-key .im-chat-msg-logs
				
		var _appendKey = ".msg-group-key .msg-group-val-"+msgObj.groupId;
		var _scrollTopKey = ".layim-chat-logs-main.msg-group-key";	
		$(_appendKey).append(html);			
		$(_scrollTopKey).scrollTop($(_scrollTopKey)[0].scrollHeight);
	}
};